<?php
/**
 * @user:andy
 * @date:2021/6/23
 * @company:http://www.sjdphp.com
 * @desc: 菜单类
 */

namespace app\admin\controller;


use app\common\controller\BackBaseController;
use app\common\model\Menu as MenuModel;
use app\common\server\Json;
use app\common\server\SystemLog;
use app\common\server\Tree;
use app\admin\validate\MenuValidate;
use app\admin\business\MenuBus;

class Menu extends BackBaseController
{

    protected $model_id = 'menu_id';
    protected $model_status = 'menu_status';

    public function lists(){
        if($this->request->isAjax()){
            try{
                $lists = MenuModel::where('menu_status','<>',config("project.mysql_code.table_delete"))
                        ->field("menu_id,menu_name,menu_icon,menu_path,menu_sort,menu_status,menu_type,operator,pid")
                        ->order("menu_sort desc")
                        ->select();
                $count = MenuModel::where('menu_status','<>',config("project.mysql_code.table_delete"))->count();
            }catch(\Exception $e){
                SystemLog::insertLog($e->getMessage());
            }
           return json(array("code" => 0,'msg' => '获取成功','count' => $count,'data' => $lists));
        }else{
            return view();
        }
    }

    public function add(){
        if($this->request->isPost()){
            $param = $this->request->param();

            ## 验证参数
            $validate = new MenuValidate();
            if(!$validate->scene("add")->check($param)) return Json::error($validate->getError());

            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 判断是否存在路径
            if(!empty($param["menu_path"])){
                ## 将字符串转为数组
                $path = explode("/",$param["menu_path"]);
                if(is_array($path)){
                    $param["menu_model"] = $path[1];
                    $param["menu_controller"] = $path[2];
                    $param["menu_action"] = $path[3];
                }
            }

            ## 默认菜单状态
            $param["menu_status"] = config("project.mysql_code.table_normal");

            ## 菜单数据入库
            try{
                $res = MenuModel::create($param);
            }catch(\Exception $e){
                SystemLog::insertLog($e->getMessage());
            }

            ## 返回
            if($res){
                return Json::success('新增成功');
            }
                return Json::error("新增失败");
        }else{
            return view('',[
                'menus' => self::getMenuLists(),
            ]);
        }
    }

    public function edit(){
        if($this->request->isPost()){
            $param = $this->request->param();

            ## 验证参数
            $validate = new MenuValidate();
            if(!$validate->scene("add")->check($param)) return Json::error($validate->getError());

            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 判断是否存在路径
            if(!empty($param["menu_path"])){
                ## 将字符串转为数组
                $path = explode("/",$param["menu_path"]);
                if(is_array($path)){
                    $param["menu_model"] = $path[1];
                    $param["menu_controller"] = $path[2];
                    $param["menu_action"] = $path[3];
                }
            }

            ## 默认菜单状态
            $param["menu_status"] = config("project.mysql_code.table_normal");

            ## 更新菜单数据
            try{
                $res = MenuModel::update($param);
            }catch(\Exception $e){
                SystemLog::insertLog($e->getMessage());
            }

            ## 返回
            if($res){
                return Json::success('更新成功');
            }
            return Json::error("更新失败");
        }else{
            $menu_id = $this->request->param("menu_id",0,'intval');
            if(!empty($menu_id)){
                $info = MenuBus::getInfo(array("menu_id" => $menu_id));
            }
            return view('',[
                'info'  => $info,
                'menus' => self::getMenuLists()
            ]);
        }
    }


    /** 获取树形结构的菜单数据
     * @return array
     */
    private static function getMenuLists(){
        $lists = MenuBus::getArrayLists(array("menu_status" => config("project.mysql_code.table_normal")),"menu_id,menu_name,pid",0,0,"menu_sort desc");
        return Tree::getTree($lists);
    }
}